//
//  TopLargerScreenView.m
//  NaRiLSVMT
//
//  Created by jmsre on 14-8-14.
//  Copyright (c) 2014年 helloyouth.org. All rights reserved.
//

#import "TopLargerScreenView.h"
#import "SVGloble.h"
#import "AllNodesInScrollView.h"
#import "RootScreenView.h"
#import "TopScreenView.h"

#define POSITIONID (int)(scrollView.contentOffset.x/(Screen_Width-20))
//滑条宽度
#define CONTENTSIZEX (400)

extern  NSInteger   lastFlowNumber;//前一个处理的流程No.或者当前

extern  BOOL    currentFlowIsPlaying;//是否是当前流程在播放

@implementation TopLargerScreenView


@synthesize viewNameArray;


//+ (TopLargerScreenView *)shareInstance {
//    static TopLargerScreenView *_instance;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        _instance=[[self alloc] initWithFrame:CGRectMake(0, 40, Screen_Width-20, 430-40)];
//    });
//    return _instance;
//}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
//        self.backgroundColor = [UIColor lightGrayColor];
        self.backgroundColor = ClearColor;
        self.pagingEnabled = YES;
        self.userInteractionEnabled = YES;
        self.bounces = NO;
        
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        
        userContentOffsetX = 0;
    }
    return self;
}

- (void)initWithViews
{
    /*******************************************************/
    self.delegate = self;
    self.backgroundColor = ClearColor;
    self.pagingEnabled = YES;
    self.userInteractionEnabled = YES;
    self.bounces = NO;
    
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    
    userContentOffsetX = 0;
    [self setContentOffset:CGPointMake(0, 0)];
    /*******************************************************/
    
//    for (int i = 0; i < [viewNameArray count]; i++) {
//        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0+(Screen_Width-20)*i, 0, Screen_Width-20, 100)];
//        label.textAlignment = NSTextAlignmentCenter;
//        label.font = [UIFont boldSystemFontOfSize:50.0];
//        label.tag = 200 + i;
//        if (i == 0) {
//            label.text = [viewNameArray objectAtIndex:i];
//        }
//        [self addSubview:label];
//    }
    self.contentSize = CGSizeMake((Screen_Width-20)*[viewNameArray count], 430-40);
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    userContentOffsetX = scrollView.contentOffset.x;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (userContentOffsetX < scrollView.contentOffset.x) {
        isLeftScroll = YES;
    }
    else {
        isLeftScroll = NO;
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    
    if ([allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"playFlag"] intValue] == 1) {
        return;
    }
    
    if(userContentOffsetX == (Screen_Width-20)*([viewNameArray count]-1) && scrollView.contentOffset.x == (Screen_Width-20)*([viewNameArray count]-1))
    {
        [scrollView setContentOffset:CGPointMake(0, 0)];//这个调整 TopScrollView按钮的滑到第一页
        userContentOffsetX = 0;
        
        [self setContentOffset:CGPointMake(0*(Screen_Width-20), 0) animated:YES];//这个调整 RootScrollView到第一页
        
    }
    //调整顶部滑条按钮状态
    [self adjustTopScrollViewButton:scrollView];
    
//    [self loadData];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
//    [self loadData];
}

//-(void)loadData
//{
//    CGFloat pagewidth = self.frame.size.width;
//    int page = floor((self.contentOffset.x - pagewidth/viewNameArray.count)/pagewidth)+1;
//    UILabel *label = (UILabel *)[self viewWithTag:page+200];
//    label.text = [NSString stringWithFormat:@"%@",[viewNameArray objectAtIndex:page]];
//}

//滚动后修改顶部滚动条
- (void)adjustTopScrollViewButton:(UIScrollView *)scrollView
{
    /***********************节点ID和按钮ID相对应************************/
    //根据节点ID获取此节点中，屏幕的信息:
    NSLog(@"%d",POSITIONID);
    TopLargerScreenView    *topLargerScr = allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"topLargerScr"];
    [topLargerScr.delagateTopLargerForTap HandleTapNodeButton:self ButtonIndex:@(POSITIONID)];
    /****************************************************************/
    AllNodesInScrollView    *bottomSmaPic = allFlowDataArr[lastFlowNumber][currentQueryingflowId][@"bottomSmaPic"];
    [bottomSmaPic setButtonUnSelect];
    bottomSmaPic.scrollViewSelectedChannelID = POSITIONID+100;
    [bottomSmaPic setButtonSelect];
    [bottomSmaPic setScrollViewContentOffset];
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
